using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Deepcode.Flux.Core.Collections;

public partial class Setup_Install_Panels_SetupSuperUser : Deepcode.Flux.Core.UI.Abstract.SetupWizardBase
{
	protected override Panel Base_ErrorContainer{ get{ return this.localErrorHost; }}
	protected override Label Base_ErrorLabel { get { return this.localErrors; } }

	protected void Page_Load(object sender, EventArgs e)
	{
	}

	/// <summary>
	/// Form is being populated
	/// </summary>
	/// <param name="settings"></param>
	public override void Initialise(Deepcode.Flux.Core.Collections.BaseDictionary settings)
	{
		base.Initialise(settings);

		// Load field content from the dictionary
		WA_Username.Text = settings["FluxAuth_WA_Username"];
		WA_Email.Text = settings["FluxAuth_Email"];
		FA_Email.Text = settings["FluxAuth_Email"];

		if (settings["FluxAuthMode"] == "FORMS")
		{
			FluxAuth_Windows.Checked = false;
			FluxAuth_Forms.Checked = true;
			PanelWinAuth.Visible = false;
			PanelFormAuth.Visible = true;
		}
		else
		{
			FluxAuth_Windows.Checked = true;
			FluxAuth_Forms.Checked = false;
			PanelWinAuth.Visible = true;
			PanelFormAuth.Visible = false;
		}
	}

	/// <summary>
	/// Form is being saved
	/// </summary>
	/// <param name="settings"></param>
	/// <returns></returns>
	public override bool FinishForm(Deepcode.Flux.Core.Collections.BaseDictionary settings)
	{
		string errors = "";
		// Load out fields to setup dictionary
		settings["FluxAuthMode"] = (FluxAuth_Windows.Checked ? "WINDOWS" : "FORMS");
		if (settings["FluxAuthMode"] == "WINDOWS")
		{
			settings["FluxAuth_Email"] = WA_Email.Text;
			settings["FluxAuth_WA_Username"] = WA_Username.Text;
			if ((WA_Username.Text.Length < 1) ||
				(WA_Username.Text.IndexOf("\\") == -1))
				errors += "The Windows username is not valid. It must be specified as DOMAIN\\username<BR>";
		}
		else
		{
			settings["FluxAuth_Email"] = FA_Email.Text;
			settings["FluxAuth_FA_Password"] = FA_Pass1.Text;
			if (FA_Pass1.Text.Length < 5) errors += "Password must be at least 5 characters long<BR>";
			if (FA_Pass1.Text != FA_Pass2.Text) errors += "Password and confirm password fields do not match<BR>";
		}

		if (settings["FluxAuth_Email"].Length < 5)
			errors += "Email address must be specified and must be a valid email address<BR>";

		if (errors != "") return DoLocalError(errors);
		return true;
	}

	protected void AuthCheckChange(object sender, EventArgs e)
	{
		if (FluxAuth_Windows.Checked)
		{
			PanelWinAuth.Visible = true;
			PanelFormAuth.Visible = false;
		}
		else
		{
			PanelWinAuth.Visible = false;
			PanelFormAuth.Visible = true;
		}
	}
}
